package com.ihelp.rest.utils;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.util.Assert;

/**
 * <br>
 * <b>功能：</b>详细的功能描述<br>
 * <b>日期：</b> Dec 14, 2011 <br>
 * <b>更新者：</b><br>
 * <b>日期：</b> <br>
 * <b>更新内容：</b><br>
 */
public class HtmlUtil {
	
	/**
	 * 
	 * <br>
	 * <b>功能：</b>输出json格式<br>
	 * <b>日期：</b> Dec 14, 2011 <br>
	 * @param response
	 * @param jsonStr
	 * @throws Exception
	 */
	public static void writerJson(HttpServletResponse response, String jsonStr) {
		response.setContentType("application/json");  
		response.setCharacterEncoding("utf-8"); 
		writer(response, jsonStr);
	}
	
	/**
	 * 文本数据
	 * @param response
	 * @param text
	 */
	public static void writerText(HttpServletResponse response, String text) {
		response.setCharacterEncoding("utf-8"); 
		writer(response, text);
	}
	
	/*public static void writerJson(HttpServletResponse response, Object object){
			try {
				response.setContentType("application/json");
				writer(response, JSONUtil.toJSONString(object));
			} catch (JSONException e) {
				e.printStackTrace();
			}
	}*/
	
	/**
	 * 
	 * <br>
	 * <b>功能：</b>输出HTML代码<br>
	 * <b>日期：</b> Dec 14, 2011 <br>
	 * @param response
	 * @param htmlStr
	 * @throws Exception
	 */
	public static void writerHtml(HttpServletResponse response,String htmlStr) {
		response.setContentType("text/html");  
		response.setCharacterEncoding("utf-8"); 
		writer(response,htmlStr);
	}
	
	private static void writer(HttpServletResponse response, String str){
		try {
			//设置页面不缓存
			/*response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setCharacterEncoding("UTF-8");*/
			PrintWriter out= null;
			out = response.getWriter();
			out.write(str);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	} 
	
	
	
	public static void sendDirectToClient(HttpServletResponse response, String s) {
		Assert.notNull(response);
		
		String charsetPrefix = org.springframework.web.util.WebUtils.CONTENT_TYPE_CHARSET_PREFIX;
		String contentHead =  "text/json"  + charsetPrefix + "UTF-8";
		response.setContentType(contentHead);
		try {
			PrintWriter writer = response.getWriter();
			writer.write(s);
			writer.flush();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}
